/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IO;

/**
 *
 * @author Eagle
 */
public class calcClass implements Runnable {
    Thread t;
    int nTotalPre,nGluPre,nTotalSlot,gluConn,gabaConn,nTotalConn;
    int dgrp,tgrp,qgrp;
    resultClass result;

    public void run(){

        cellClass[] cell=new cellClass[nTotalPre];
        for (int i=0;i<nTotalPre;i++){
            cell[i]=new cellClass();
        }
//        cellClass cell2=new cellClass();
        //mark the positive ones

        int[] shuffleArray=new int[nTotalPre];
        for (int i=0;i<nTotalPre;i++)
        {
            shuffleArray[i]=i;
        }

        myCommonTask.shuffleArray(shuffleArray);


        for (int i=0;i<nGluPre;i++) {
            cell[shuffleArray[i]].setGlu();
        }

        int[] shuffleConnAry=new int[nTotalSlot];
        for (int i=0;i<nTotalSlot;i++)
        {
            shuffleConnAry[i]=i;
//            dp(nTotalSlot+" "+Integer.toString(i));
        }

        myCommonTask.shuffleArray(shuffleConnAry);

        int atShuffleAry=0,currGluConn=gluConn,currGABAConn=gabaConn;
        int currlink;

        for (int i=0;i<nTotalConn;){
//            //dispatch all link;
            currlink=shuffleConnAry[atShuffleAry++];
            //test for positive pre, first find out pre cell sn
            linkInfoClass linkInfo=new linkInfoClass(currlink,dgrp,tgrp,qgrp);
            int currCell=linkInfo.pre();

            if (cell[currCell].isGlu()&&currGluConn>0) {
                //currCell is positive,add a output to currCell
                cell[currCell].addOutput();
                cell[currCell].setGroupSize(linkInfo.grpSize());
                cell[linkInfo.post()].addGluInput();
                currGluConn--;
                i++;
            }else if((!cell[currCell].isGlu())&&currGABAConn>0){
                cell[currCell].addOutput();
                cell[currCell].setGroupSize(linkInfo.grpSize());
                cell[linkInfo.post()].addGABAInput();
                currGABAConn--;
//                dp(currCell+" "+linkInfo.post());
                i++;
            }
//            dp(i+" "+currGluConn+" "+currGABAConn+" "+atShuffleAry);

        } //all links are dispatched
        //sum up distribution, write result array
        for (int i=0;i<nTotalPre;i++){
            //dp (Integer.toString(i));
            if (cell[i].isGlu()){
                result.addGluOutput(cell[i].output());
                result.addGluInputOnGlu(cell[i].gluInput());
                result.addGABAInputOnGlu(cell[i].gabaInput());
                result.addGluClusterCoe(cell[i].groupSize(), cell[i].output());
            }
            else{
                result.addGABAOutput(cell[i].output());
                result.addGluInputOnGABA(cell[i].gluInput());
                result.addGABAInputOnGABA(cell[i].gabaInput());
                result.addGABAClusterCoe(cell[i].groupSize(), cell[i].output());
            }
        }//result of this simulation is written in result array.
    }

    calcClass(resultClass Result, constClass c){
        nTotalPre=c.totalPre;
        nGluPre=c.gluPre;
        nTotalSlot=c.totalSlot;
        gluConn=c.gluConn;
        gabaConn=c.gabaConn;
        nTotalConn=c.totalConn;
        result=Result;
        dgrp=c.dgrp;
        tgrp=c.tgrp;
        qgrp=c.qgrp;

        t=new Thread(this, "Child Thread");
        t.start();
        //instance cells
    }
}
